/**
 * [8] 字符串转换整数 (atoi)
 *
 * @format
 * @lc app=leetcode.cn id=8 lang=javascript
 */

// @lc code=start
/**
 * @param {string} s
 * @return {number}
 */
var myAtoi = function (s) {
    const [INT_MIN, INT_MAX, MAX_LEN] = [-(2 ** 31), 2 ** 31 - 1, 11]
    let [i, sign, start, end] = [-1, 0, -1, s.length]
    i = s.search(/[\+\-]/)
    i === -1 || s[i] === "+" ? (sign = 1) : (sign = -1)
    start = s.search(/\d/)
    if (start === -1 || (i !== -1 && start !== i + 1)) {
    }
    const num = s.slice(start, end)
    let sum = 0
    for (let i = 0; i < num.length; i++) {
        sum += sign * num[i] * 10 ** (num.length - i - 1)
        if (sum >= INT_MAX) return INT_MAX
        if (sum <= INT_MIN) return INT_MIN
    }
    return sum
}
// debugger
// console.log(myAtoi("+1"))
/* var myAtoi = function (s) {
    return parseInt(s, 10)
} */
// @lc code=end
